package com.yanqu.road.server.cmd.rank;

import com.yanqu.road.entity.enums.eRankType;
import com.yanqu.road.entity.servercenter.ServerGroup;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.entity.union.cross.CrossUnionServerGroup;
import com.yanqu.road.logic.redis.RedisManger;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.union.UnionActivityGroupProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.CrossUnionServerGroupMgr;
import com.yanqu.road.server.manger.ServerGroupMgr;
import com.yanqu.road.server.manger.rank.RankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;
import redis.clients.jedis.Jedis;

import java.util.Objects;

@Cmd(code = Protocol.S_UNION_RANK_LIST, desc = "获取商会排行榜数据")
public class UnionRankListCmd extends AbstractCommand {

    private int type;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RankProto.UnionRankReqMsg reqMsg = RankProto.UnionRankReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getRankType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData(GamePlayer player) {
        String unionUid = player.getUserInfo().getUnionUid();
        if (eRankType.UnionEarnSpeed.getValue() == type){
            //跨服商会分组是否有开启
            CrossUnionServerGroup group = CrossUnionServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
            if(group != null && group.getState() == 1) {
                if(player.isRequestToFast()){//访问太频繁了，不给回包了
                    return;
                }
                RankProto.CrossGroupUnionRankReqMsg.Builder req = RankProto.CrossGroupUnionRankReqMsg.newBuilder();
                req.setGroupId(ServerGroupMgr.getGroupIdByServerId(GameServer.getInstance().getServerId()));
                req.setUnionId(unionUid);
                player.sendPacket(CrossProtocol.C_CROSS_UNION_GROUP_UNION_RANK, req);
                return;
            }
        }
        if (eRankType.CrossUnionEarnSpeed.getValue() == type) {
            ServerGroup serverGroup = ServerGroupMgr.getServerGroupByServerId(GameServer.getInstance().getServerId());
            if(null != serverGroup && serverGroup.isCrossRank()) {
                RankProto.CrossGroupUnionRankReqMsg.Builder req = RankProto.CrossGroupUnionRankReqMsg.newBuilder();
                req.setGroupId(serverGroup.getGroupId());
                req.setUnionId(unionUid);
//                player.sendChannelPacket(CrossProtocol.C_CROSS_GROUP_UNION_RANK, req);
                player.sendPacket(CrossProtocol.C_CROSS_SERVER_GROUP_UNION_RANK, req);
            }
            return;
        } else {
            //原有本服商会排行
            RankProto.UnionRankRespMsg.Builder respMsg = RankProto.UnionRankRespMsg.newBuilder();
            RankProto.UnionRankListMsg.Builder listMsg = RankMgr.getUnionRankList().getRankListMsg();
            if (!StringUtils.isNullOrEmpty(unionUid)) {
                respMsg.setMyRank(RankMgr.getUnionRankList().unlockGetMyRank(unionUid));
            } else {
                respMsg.setMyRank(-1);
            }
            respMsg.setRankList(listMsg);
            player.sendPacket(Protocol.U_UNION_RANK_LIST, respMsg);
        }
    }

    private int getMyRank(String unionUid, RankProto.UnionRankListMsg.Builder listMsg) {
        if (!StringUtils.isNullOrEmpty(unionUid)) {
            int index = 1;
            int myRank = -1;
            for (RankProto.UnionRankTempMsg tempMsg : listMsg.getRankListList()) {
                if (Objects.equals(tempMsg.getUnionUid(), unionUid) && tempMsg.getUnionBaseData().getServerId() == GameServer.getInstance().getServerId()) {
                    myRank = index;
                    break;
                }
                index++;
            }
            return myRank;
        } else {
            return -1;
        }
    }
}
